export default {
    name: "Index",
    data() {
        return {
            defaultActive:'/mydevice',
            elmenu: 'key',
            activeTab: '/mydevice',
            menuData: [
                {
                    "mName": "加载中...",
                    "action": "",
                    "mid": "",
                    "pId": "",
                    "indexse": '',
                    "show": "1",
                    "icon": '',
                    "level": "1",
                    "operate": [],
                    "children": [{
                        "mName": "加载中...",
                        "action": "",
                        "mid": "",
                        "pId": "",
                        "icon": '',
                        "indexse": '',
                        "show": "1",
                        "level": "2",
                        "operate": [],
                        "children": []
                    }]
                }
            ],
            navMenu: [],
            userInfo: '',
            newsNum: 0,
            transitionMain: '',
            transitionAside: '',
            handlePassDialog:false,
            passForm:{
               newPass:'',
               newPassAgain:'',
            },
            newsTap: (new Date()).getTime(),
        };
    },
    computed: {
        newsTip() {
            return "共有" + this.newsNum + "条未读报警";
        },
        // defaultActive() {
        //     return '/' + this.$route.path.split('/').reverse()[0];
        // }
    },
    watch: {
        //使用watch监听$router的变化
        $route(to, from) {
            this.transitionMain = 'fade-transform';
        },
        navMenu(val, oldVal) {
            this.transitionAside = "fade";
        },
        handlePassDialog:function(newVal,oldVal){
           if(oldVal){
                this.newPass = '';
            }
        }
    },
    created() {
        this.userInfo = JSON.parse(window.localStorage.getItem('userInfo'));
        //获取所以菜单
        this.$http.get('/loginProductController/queryMenuByRole')
            .then((res) => {
                res.rows.sort(this.compare("indexse"));
                this.menuData = res.rows;
                for (var i = 0; i < this.menuData.length; i++) {
                    var val = this.menuData[i];
                    if (val.show) {
                        this.navMenu = val.children;
                        //console.log(this.navMenu);
                        this.activeTab = val.mid;
                        break;
                    }
                }
                window.localStorage.setItem('roleMenu', JSON.stringify(res.rows));
            }).catch((err) => {
            console.log(err);
        });
        //第一个路由对象
        this.$router.push({ path: '/mydevice' });
        //this.activeNav = this.$route.path;
    },
    mounted() { },
    methods: {
        recordJump() {
            this.newsTap = (new Date()).getTime();
        },
        handleCommand(command) {
          if (command === "1") {
             this.$http.get('/loginProductController/toLogin')
               .then((res) => {
                  if (res.status == 200) {
                     //触发setIsLogin方法改变vuex中isLogin的值，
                     this.$store.commit("setIsLogin", false);
                     //改变localStorage中的isLogin的值
                     window.localStorage.setItem("isLogin", false);
                     //移除token
                     window.localStorage.removeItem('token');
                      this.$notify({
                        title: '退出登录成功',
                        message: res.message,
                        type: 'success'
                      });
                      this.$router.push({
                            path: '/login'
                      });
                    }
                 }).catch((err) => {
                 console.log("errs", err);
              })
            }
        },
        /**修改密码*/
        modifyPass() {
            var passRule = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/; //密码正则
            if (!passRule.test(this.passForm.newPass)) {
                this.$message.error('密码由英文和数字组合,6-10位');
                return;
            }
            if (this.passForm.newPassAgain == "") {
                this.$message.error('请再次输入密码确认');
                return;
            }
            if (this.passForm.newPass != this.passForm.newPassAgain) {
                this.$message.error('密码不一致');
                return;
            }
            this.$http.get('/userController/updatePasswordByUId?uId=' + this.userInfo.uId + '&password=' + this.passForm.newPass)
              .then((res) => {
                 if (res.status == 200) {
                    this.$message({
                        type: 'success',
                        message: res.message
                     });
                    this.handlePassDialog = false;
                    return;
                  }
                    this.$message.error(res.message);
                })
              .catch((res) => {
                console.log(res);
            })
        },
        handleOpen(Key, keyPath) {
            //console.log(Key,KeyPath);
        },
        handleClose(Key, keyPath) {
            //console.log(Key,KeyPath);
        },
        handleSelect(Key, keyPath) {
            //console.log(Key,KeyPath);
            //this.activeNav = key;
        },
        compare(property) {
            return function (a, b) {
                var value1 = a[property];
                var value2 = b[property];
                return value1 - value2
            }
        },
        handleTab(tab, event) {
            for(var i =0;i<this.menuData.length;i++){
                var val = this.menuData[i];
                if(val.mid == tab.name){
                    this.navMenu = val.children;
                    break;
                }
            }
        },
        navJudge(obj) {
            if (obj.show) {
                for (var i = 0; i < obj.children.length; i++) {
                    if (obj.children[i].show) {
                        return 0;
                        break;
                    }
                }
                return 1;
            }
            return 0;
        }
    }
};